package com.school.hjt.schoolclasscard.utlis;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.weather.LocalDayWeatherForecast;
import com.amap.api.services.weather.LocalWeatherForecast;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;

import java.util.List;

/**
 * 获取天气定位
 * Created by xqx on 2017/11/15.
 */

public class LocationUtlis {
    private  AMapLocationClient mLocationClient;
    private Context context;

    public LocationUtlis(Context context) {
        this.context = context;
    }

    public  void Location(){
        //初始化定位
       mLocationClient = new AMapLocationClient(context);
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        mLocationOption.setOnceLocation(true);
        //获取最近3s内精度最高的一次定位结果：
        mLocationOption.setOnceLocationLatest(true);
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }
    //声明定位回调监听器
    public  AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation.getErrorCode() == 0) {
                //可在其中解析amapLocation获取相应内容。
                String city=aMapLocation.getCity();
                String substring = city.substring(0, 2);
                Log.e("ATG",substring);
                WeatherSearchQuery mquery = new WeatherSearchQuery(substring, WeatherSearchQuery.WEATHER_TYPE_FORECAST);
                WeatherSearch mweathersearch=new WeatherSearch(context);
                mweathersearch.setOnWeatherSearchListener(new WeatherSearch.OnWeatherSearchListener() {
                    @Override
                    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
                        if (i == 1000) {
                            if (localWeatherLiveResult != null&&localWeatherLiveResult.getLiveResult() != null) {
                                LocalWeatherLive liveResult = localWeatherLiveResult.getLiveResult();
//                                Intent intent=new Intent();
//                                intent.putExtra(Constant.WEATHER,liveResult.getCity()+" "+liveResult.getWeather()+"  温度:"+liveResult.getTemperature()+"°");
//                                intent.setAction(Constant.WEATHER);
//                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                context.sendBroadcast(intent);
                                Log.e("ATG",liveResult.getWeather()) ;
                            }
                        }
                    }

                    @Override
                    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {
                            if (i==1000){
                                LocalWeatherForecast forecastResult = localWeatherForecastResult.getForecastResult();
                                List<LocalDayWeatherForecast> weatherForecast = forecastResult.getWeatherForecast();
                                Intent intent=new Intent();
                                intent.putExtra(Constant.WEATHER,weatherForecast.get(0).getDayWeather()+" "+weatherForecast.get(0).getDayTemp()+"-"+weatherForecast.get(0).getNightTemp()+"℃");
                                intent.setAction(Constant.WEATHER);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.sendBroadcast(intent);
//                                Log.e("ATG",weatherForecast.get(0).getDayWeather()) ;
//                                Log.e("ATG",weatherForecast.get(0).getDate()) ;
//                                Log.e("ATG",weatherForecast.get(0).getDayTemp()) ;
//                                Log.e("ATG",weatherForecast.get(0).getWeek()) ;
//                                Log.e("ATG",weatherForecast.get(0).getNightTemp()) ;
//                                Log.e("ATG",weatherForecast.get(0).getDayWindDirection()) ;
//                                Log.e("ATG",weatherForecast.get(0).getDayWindPower()) ;
                            }
                    }
                });
//                mweathersearch.setOnWeatherSearchListener(new WeatherSearch.OnWeatherSearchListener() {
//                    @Override
//                    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
//
//                    }
//
//                    @Override
//                    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {
//
//                    }
//                });
                mweathersearch.setQuery(mquery);
                mweathersearch.searchWeatherAsyn();
            }else{
                mLocationClient.startLocation();
            }
        }
    };
}
